<?php

namespace Biz\service;

//use Biz\service\search\Goods as searchGoods;
use Biz\cache\PresetCondition as cPresetCondition;
use Biz\model\ShopCategoryClassify as mShopCategoryClassify;
use Biz\service\brand\Brand;
use Biz\service\esSearch\Wiki as esWiki;
use Biz\service\wiki\Wiki;
use Doraemon\model\Elasticsearch\EsWikiListModel;
use Doraemon\tools\HhzClient;

class FilterPage
{


    //优惠卷凑单列表
    public static function togetherOrder()
    {

        $allCategoryList = Category::getAllCategoryList();
        $selectList = [];
        $classify = [
            'type' => 'category',
            'type_desc' => '分类',
            'list' => []
        ];
        foreach ($allCategoryList as $categoryList) {
            $classify['list'][] = [
                'id' => $categoryList['id'],
                'title' => $categoryList['title'],
            ];
        }
        $selectList[] = [
            'type' => 'price_range',
            'type_desc' => '价格区间',
            'list' => [
                ["id" => "1", "title" => null],
                ["id" => "2", "title" => null]
            ]
        ];
        $selectList[] = $classify;
        return $selectList;
    }

    //分类页筛选列表
    private static function secFilterPage($ClassifyId)
    {
        $key = cPresetCondition::getKey(cPresetCondition::TYPE_CATEGORY_ID, $ClassifyId);
        $Rst = cPresetCondition::getItem($key);

        // 如果缓存存在的话，直接查询，否则的话，在走下面的流程
        if (!$Rst) {
            $classifyInfo = ShopClassify::quiltGetClassifyById($ClassifyId);

            if (!isset($classifyInfo['deep'])) {
                throw new \Exception('不存在的的分类ID');
            }
            if (!in_array($classifyInfo['deep'], [mShopCategoryClassify::DEEP_THIRD_CLAORCATE, mShopCategoryClassify::DEEP_SECOND_CLASSIFY, mShopCategoryClassify::DEEP_FIRST_CLASSIFY])) {
                throw new \Exception('不存在的的分类ID');
            }
            $cateIds = [];
            if ($classifyInfo['deep'] == mShopCategoryClassify::DEEP_SECOND_CLASSIFY || $classifyInfo['deep'] == mShopCategoryClassify::DEEP_THIRD_CLAORCATE) {
                $cateIds = array_keys($classifyInfo['cate_list']);
            } else if ($classifyInfo['deep'] == mShopCategoryClassify::DEEP_FIRST_CLASSIFY) {
                foreach ($classifyInfo['classifies'] as $secClassify) {
                    $cateIds = array_merge($cateIds, array_keys($secClassify['cate_list']));
                    foreach ($secClassify['classifies'] as $classify) {
                        $cateIds = array_merge($cateIds, array_keys($classify['cate_list']));
                    }
                }
            }
            $category = [];
            $Rst = [
                [
                    "type" => "price_range",
                    "type_desc" => "价格区间",
                    "list" => [
                        ["id" => "1", "title" => null],
                        ["id" => "2", "title" => null]
                    ]
                ]
            ];
            if ($classifyInfo['deep'] == mShopCategoryClassify::DEEP_SECOND_CLASSIFY || $classifyInfo['deep'] == mShopCategoryClassify::DEEP_THIRD_CLAORCATE) {
                foreach ($classifyInfo['classifies'] as $thirdClassifyId => $classify) {
                    $cateIds = array_merge($cateIds, array_keys($classify['cate_list']));
                    $category[] = [
                        'id' => $classify['id'],
                        'title' => $classify['classify_name']
                    ];
                }
            }
            if ($category) array_push($Rst, [
                "type" => "category",
                "type_desc" => "分类",
                "list" => $category
            ]);
            if ($cateIds) {
//                $results = esWiki::getList(['category_id' => $cateIds], esWiki::SCENES_APP_PRESET_CONDITION, 0, 1, 20, ['brand_id']);
                $results = EsWikiListModel::getList(['category_id' => $cateIds], EsWikiListModel::SCENES_APP_PRESET_CONDITION, 0, 1, 20, ['brand_id']);
                if (!$results['is_over']) {
//                    $results = esWiki::getList(['category_id' => $cateIds], esWiki::SCENES_APP_PRESET_CONDITION, 0, 1, $results['total'] > 10000 ? 10000 : $results['total'], ['brand_id']);
                    $results = EsWikiListModel::getList(['category_id' => $cateIds], EsWikiListModel::SCENES_APP_PRESET_CONDITION, 0, 1, $results['total'] > 10000 ? 10000 : $results['total'], ['brand_id']);
                }
                $brandIds = $results['rows'];
                $brandIds = array_unique(array_filter($brandIds));
                $brandList = $brandIds ? Brand::getBrandInfosCache($brandIds) : [];
                $brand = [];
                foreach ($brandList as $item) {
                    if ($item['is_test'] == 0) {//非测试号
                        $brand[] = [
                            'id' => $item['id'],
                            'title' => $item['brand_name'],
                        ];
                    }
                }
                if ($brand) array_push($Rst, [
                    "type" => "brand",
                    "type_desc" => "品牌",
                    "list" => $brand
                ]);
            }
            cPresetCondition::setItem($key, $Rst);
        }

        return $Rst;
    }

    private static function tagPageSearch($tag)
    {
        $key = cPresetCondition::getKey(cPresetCondition::TYPE_TAGS, base64_encode($tag));
        $Rst = cPresetCondition::getItem($key);
        if (!$Rst) {
//            $results = esWiki::getList(['tag' => $tag], esWiki::SCENES_APP_PRESET_CONDITION);
            $results = EsWikiListModel::getList(['tag' => $tag], EsWikiListModel::SCENES_APP_PRESET_CONDITION);
            if (!$results['is_over']) {
//                $results = esWiki::getList(['tag' => $tag], esWiki::SCENES_APP_PRESET_CONDITION, 0, 1, $results['total']);
                $results = EsWikiListModel::getList(['tag' => $tag], EsWikiListModel::SCENES_APP_PRESET_CONDITION, 0, 1, $results['total']);
            }
            $goodsList = Wiki::getWikiBaseInfos($results['rows']);
            $brandIds = $goodsList ? array_filter(array_unique(array_column($goodsList, 'brand_id'))) : [];
            $brandList = $brandIds ? Brand::getBrandInfosCache($brandIds) : [];
            $brand = [];
            $Rst = [
                [
                    "type" => "price_range",
                    "type_desc" => "价格区间",
                    "list" => [
                        ["id" => "1", "title" => null],
                        ["id" => "2", "title" => null]
                    ]
                ]
            ];
            foreach ($brandList as $item) {
                if ($item['is_test'] == 0) {//非测试号
                    $brand[] = [
                        'id' => $item['id'],
                        'title' => $item['brand_name'],
                    ];
                }
            }
            if ($brand) array_push($Rst, [
                "type" => "brand",
                "type_desc" => "品牌",
                "list" => $brand
            ]);
            cPresetCondition::setItem($key, $Rst);
        }
    }

    public static function jointSearchPage(array $params)
    {
        if (isset($params['category_id']) && $params['category_id']) {
            $filterPage = self::secFilterPage($params['category_id']);
            foreach ($filterPage as $key => $row) {
                if (version_compare(HhzClient::getClientVersion(), "5.20.0", ">=") && $row['type'] == 'category') {
                    unset($filterPage[$key]);
                }
            }
            $filterPage = array_values($filterPage);
        } else if (isset($params['tag']) && $params['tag']) {
            $filterPage = self::tagPageSearch($params['tag']);

        } else {
            $filterPage = self::togetherOrder();
        }

        return $filterPage;
    }
}